package main

import "fmt"

//写
func writeData(intChan chan int) {
	for i := 0; i < 50; i++ {
		//写入数据
		intChan <- i
		fmt.Println("writeData", i)
	}
	close(intChan)
}

//读
func readData(intChan chan int, exitChan chan bool) {
	for i := range intChan {
		fmt.Println("readData", i)
	}
	//读完后发送退出信号
	exitChan <- true
	close(exitChan)
}

func main() {
	//定义两个channel
	//当channel长度小于要写入的元素个数时，只要这个channel有被一直读，则不会死锁。
	intChan := make(chan int, 10)
	exitChan := make(chan bool, 1)
	//开启两个协程
	go writeData(intChan)
	go readData(intChan, exitChan)

	//阻塞主线程
	for {
		//如果读到值了，则ok为true，读完了则为false
		_, ok := <-exitChan
		if ok {
			break
		}
	}
}
